<template>
  <div class="dictionaryController">
    <div class="title"><h3>自定义选项值</h3></div>

    <div>
      <label
        >参数:
        <el-input v-model="dic_Name"></el-input>
      </label>
    </div>
    <div
      class="type"
      v-for="(item, index) in list"
      :key="index"
      @click="getType($event)"
    >
      {{ item.dic_Name }}
    </div>
    <div class="btn">
      <el-button type="primary" @click="fixSubmit">修改</el-button>
    </div>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import { mapState } from "vuex";
export default {
  data() {
    return {
      type: "",
      dic_Name: "",
      dic_Id: "",
      list: [],
    };
  },
  computed: {
    ...mapState("userManage", ["customerType", "customerOrigin"]),
  },
  methods: {
    ...mapActions(["userManage/getDicListActions", "userManage/fixDicActions"]),
    getType(e) {
      this.dic_Name = e.target.innerText;
      this.list.forEach((item) => {
        if (item.dic_Name === e.target.innerText) {
          this.dic_Id = item.dic_Id;
        }
      });
    },
    fixSubmit() {
      this["userManage/fixDicActions"]({
        dic_Name: this.dic_Name,
        dic_Id: this.dic_Id,
      }).then(() => {
        this["userManage/getDicListActions"](this.type)
          .then(() => {
            console.log("返回客户类型数据成功");
            if (this.type === "客户类型") {
              this.list = this.customerType;
            } else {
              this.list = this.customerOrigin;
            }
          })
          .catch(() => {
            console.log("请求失败");
          });
      });
    },
  },
  created() {
    this.type = this.$route.query.val;
    if (this.type === "客户类型") {
      this.list = this.customerType;
    } else if (this.type === "客户来源") {
      this.list = this.customerOrigin;
    }
  },
};
</script>

<style lang="scss" scoped>
.title {
  width: 500px;
  height: 50px;
  h3 {
    text-align: center;
    line-height: 50px;
  }
}
.dictionaryController {
  width: 500px;
  margin: 20px auto 50px;
  padding: 20px;
}

.el-input {
  width: 300px;
}
label {
  display: block;
  margin: 30px auto;
}

.btn {
  margin-left: 40px;
  margin-top: 40px;
}
.type {
  height: 50px;
  border-bottom: 1px solid #ccc;
  line-height: 50px;
  text-indent: 10px;
  cursor: pointer;
}
</style>